using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Web;
using MySql.Data.MySqlClient;
namespace LdExpress
{
    /// <summary>
    /// 
    /// </summary>
    public class LocalChargesDetail
    {
        public long ID = 0;
        public long LocalChargesID = 0;
        public string ChargeCode = "";
        public string UnitCode = "";
        public string CurrCode = "";
        public float BuyPrice = 0;
        public float SellPriceNoVat = 0;
        public float SellPriceWithVat = 0;
        public string Note = "";
        public DateTime LastUpdate = DateTime.Now;
        public int ImpExpType = 0;
        public const int EXPORTATION = 1;
        public const int IMPORTATION = 2;
        public LocalChargesDetail()
        {
        }
        public LocalChargesDetail(long lID)
        {
            this.ID = lID;
        }
        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            if (ID <= 0) // new object, we insert new record to database
            {
                // build SQL statement
                sqlCommand = "Insert into tbl_LocalChargesDetail (" +
                             "LocalChargesID," +
                             "ChargeCode," +
                             "UnitCode," +
                             "CurrCode," +
                             "BuyPrice," +
                             "SellPriceNoVat," +
                             "SellPriceWithVat," +
                             "Note," +
                             "LastUpdate," +
                             "ImpExpType) " +
                         " values(" +
                             LocalChargesID.ToString() + "," +
                             "'" + DBModule.RefineString(ChargeCode) + "'," +
                             "'" + DBModule.RefineString(UnitCode) + "'," +
                             "'" + DBModule.RefineString(CurrCode) + "'," +
                             BuyPrice.ToString() + "," +
                             SellPriceNoVat.ToString() + "," +
                             SellPriceWithVat.ToString() + "," +
                             "'" + DBModule.RefineString(Note) + "'," +
                             DBModule.RefineDatetime(LastUpdate) + "," +
                             ImpExpType.ToString() + ")";
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                sqlCommand = "Update tbl_LocalChargesDetail set " +
                             "ID=" + ID.ToString() + "," +
                             "LocalChargesID=" + LocalChargesID.ToString() + "," +
                             "ChargeCode='" + DBModule.RefineString(ChargeCode) + "'," +
                             "UnitCode='" + DBModule.RefineString(UnitCode) + "'," +
                             "CurrCode='" + DBModule.RefineString(CurrCode) + "'," +
                             "BuyPrice=" + BuyPrice.ToString() + "," +
                             "SellPriceNoVat=" + SellPriceNoVat.ToString() + "," +
                             "SellPriceWithVat=" + SellPriceWithVat.ToString() + "," +
                             "Note='" + DBModule.RefineString(Note) + "'," +
                             "LastUpdate=" + DBModule.RefineDatetime(LastUpdate) + "," +
                             "ImpExpType=" + ImpExpType.ToString() + "" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            //Buil SQL Statement
            //Run SQL Statement
            string sqlCommand = "DELETE FROM tbl_LocalChargesDetail WHERE ID = " + this.ID.ToString() + " limit 1";
            DBModule.ExecuteNonQuery(sqlCommand, cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            //Build SQL Statement
            //Run SQL Statement
            sqlCommand = "SELECT * FROM tbl_LocalChargesDetail WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID"))
                    ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("LocalChargesID"))
                    LocalChargesID = long.Parse(dr["LocalChargesID"].ToString());
                if (!dr.IsNull("ChargeCode"))
                    ChargeCode = dr["ChargeCode"].ToString();
                if (!dr.IsNull("UnitCode"))
                    UnitCode = dr["UnitCode"].ToString();
                if (!dr.IsNull("CurrCode"))
                    CurrCode = dr["CurrCode"].ToString();
                if (!dr.IsNull("BuyPrice"))
                    BuyPrice = float.Parse(dr["BuyPrice"].ToString());
                if (!dr.IsNull("SellPriceNoVat"))
                    SellPriceNoVat = float.Parse(dr["SellPriceNoVat"].ToString());
                if (!dr.IsNull("SellPriceWithVat"))
                    SellPriceWithVat = float.Parse(dr["SellPriceWithVat"].ToString());
                if (!dr.IsNull("Note"))
                    Note = dr["Note"].ToString();
                if (!dr.IsNull("LastUpdate"))
                    LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
                if (!dr.IsNull("ImpExpType"))
                    ImpExpType = int.Parse(dr["ImpExpType"].ToString());
            }
            else
            {
            }
        }
        public void Load(long localChargesID, MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            //Build SQL Statement
            //Run SQL Statement
            sqlCommand = "SELECT * FROM tbl_LocalChargesDetail WHERE LocalChargesID = " + localChargesID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID"))
                    ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("LocalChargesID"))
                    LocalChargesID = long.Parse(dr["LocalChargesID"].ToString());
                if (!dr.IsNull("ChargeCode"))
                    ChargeCode = dr["ChargeCode"].ToString();
                if (!dr.IsNull("UnitCode"))
                    UnitCode = dr["UnitCode"].ToString();
                if (!dr.IsNull("CurrCode"))
                    CurrCode = dr["CurrCode"].ToString();
                if (!dr.IsNull("BuyPrice"))
                    BuyPrice = float.Parse(dr["BuyPrice"].ToString());
                if (!dr.IsNull("SellPriceNoVat"))
                    SellPriceNoVat = float.Parse(dr["SellPriceNoVat"].ToString());
                if (!dr.IsNull("SellPriceWithVat"))
                    SellPriceWithVat = float.Parse(dr["SellPriceWithVat"].ToString());
                if (!dr.IsNull("Note"))
                    Note = dr["Note"].ToString();
                if (!dr.IsNull("LastUpdate"))
                    LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
                if (!dr.IsNull("ImpExpType"))
                    ImpExpType = int.Parse(dr["ImpExpType"].ToString());
            }
            else
            {
            }
        }
        public static void GetList(string Condition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select * from tbl_LocalChargesDetail where 1=1 ";
            if (Condition.Length > 0)
            {
                sqlCommand += Condition;
            }

            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(sqlCommand, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
        }
        public static void DoSearch(string keywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string sqlCommand = "";
            ds = null;
            // build SQL statement
            sqlCommand = "Select `ID` from tbl_LocalChargesDetail where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                sqlCommand = sqlCommand + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(sqlCommand, cn, trans);
        }
    }
}
